package org.jeecg.modules.ft.ftOrder.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.ft.ftOrder.entity.FtOrder;
import org.jeecg.modules.ft.ftOrder.entity.OrderReport;
import org.jeecg.modules.ft.ftOrder.service.IFtOrderService;
import org.jeecg.modules.ft.ftOrderDetail.entity.FtOrderDetail;
import org.jeecg.modules.ft.ftOrderDetail.service.IFtOrderDetailService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 餐饮订单
 * @Author: jeecg-boot
 * @Date:   2021-01-11
 * @Version: V1.0
 */
@Api(tags="餐饮订单")
@RestController
@RequestMapping("/ftOrder/ftOrder")
@Slf4j
public class FtOrderController extends JeecgController<FtOrder, IFtOrderService> {
	@Autowired
	private IFtOrderService ftOrderService;
	@Autowired
	private IFtOrderDetailService ftOrderDetailService;

	/**
	 * 分页列表查询
	 *
	 * @param ftOrder
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "餐饮订单-分页列表查询")
	@ApiOperation(value="餐饮订单-分页列表查询", notes="餐饮订单-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(FtOrder ftOrder,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<FtOrder> queryWrapper = QueryGenerator.initQueryWrapper(ftOrder, req.getParameterMap());
		Page<FtOrder> page = new Page<FtOrder>(pageNo, pageSize);
		IPage<FtOrder> pageList = ftOrderService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param ftOrder
	 * @return
	 */
	@AutoLog(value = "餐饮订单-添加")
	@ApiOperation(value="餐饮订单-添加", notes="餐饮订单-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody FtOrder ftOrder) {
		ftOrderService.save(ftOrder);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param ftOrder
	 * @return
	 */
	@AutoLog(value = "餐饮订单-编辑")
	@ApiOperation(value="餐饮订单-编辑", notes="餐饮订单-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody FtOrder ftOrder) {
		ftOrderService.updateById(ftOrder);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "餐饮订单-通过id删除")
	@ApiOperation(value="餐饮订单-通过id删除", notes="餐饮订单-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		ftOrderService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "餐饮订单-批量删除")
	@ApiOperation(value="餐饮订单-批量删除", notes="餐饮订单-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.ftOrderService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "餐饮订单-通过id查询")
	@ApiOperation(value="餐饮订单-通过id查询", notes="餐饮订单-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		FtOrder ftOrder = ftOrderService.getById(id);
		if(ftOrder==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(ftOrder);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param ftOrder
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, FtOrder ftOrder) {

    	//组装查询条件
		QueryWrapper<FtOrder> queryWrapper=QueryGenerator.initQueryWrapper(ftOrder,request.getParameterMap());
		LoginUser sysuser=(LoginUser) SecurityUtils.getSubject().getPrincipal();

		//获取导出数据
    	List<FtOrder> queryList=this.ftOrderService.list(queryWrapper);

    	//过滤选择数据
		String selections =request.getParameter("selections");
		List<FtOrder> ftOrderList=new ArrayList<>();
		if(selections==null){
			ftOrderList=queryList;
		}else{
			List<String> selectionList=Arrays.asList(selections.split(","));
			ftOrderList=queryList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
		}

		//组装pageList
		List<OrderReport> pageList=new ArrayList<>();
		for(FtOrder main:ftOrderList){
			OrderReport vo=new OrderReport();
			BeanUtils.copyProperties(main,vo);
			List<FtOrderDetail> ftOrderDetailList=ftOrderDetailService.orderDetailList(main.getOrderCode());
			vo.setFtOrderDetailList(ftOrderDetailList);
			pageList.add(vo);
		}

    	ModelAndView mv=new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME,"订单列表");
		mv.addObject(NormalExcelConstants.CLASS,OrderReport.class);
		mv.addObject(NormalExcelConstants.PARAMS,new ExportParams("订单列表数据", "订单表"));
		mv.addObject(NormalExcelConstants.DATA_LIST,pageList);
		return mv;

    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, FtOrder.class);
    }

}
